#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
ll arr1[100000 + 10];
ll arr2[100000 + 10];
void solve()
{
    memset(arr1, 0, sizeof(arr1));
    memset(arr2, 0, sizeof(arr2));
    ll n;
    ll r, c;
    cin >> n;
    ll i = 0;
    ll left = 2;
    ll right = 2;
    ll j = 0;
    ll k = 0;
    bool low = 0;
    bool high = 0;
    bool mid = 0;
    for (i = 0; i < n; i++)
    {
        cin >> r >> c;
        if (c == 0)
            mid = 1;
        else
        {
            if (r == 1)
            {
                arr1[j++] = c;
                if (c == -1)
                    low = 1;
                if (c == 1)
                    high = 1;
            }
            else
                arr2[k++] = c;
        }
    }
    sort(arr1, arr1 + j);
    sort(arr2, arr2 + k);
    if (j != 0)
    {
        if (arr1[0] < 0 && arr1[j - 1] > 0)
            left = 1, right = 1;
        else if (arr1[0] < 0)
            left = 1;
        else
            right = 1;
    }
    if (k != 0)
    {
        if (arr2[0] < 0 && arr2[k - 1] > 0)
            left = 1, right = 1;
        else if (arr2[0] < 0)
            left = 1;
        else
            right = 1;
    }
    for (j = 0, k = 0; arr1[j] != 0 && arr2[k] != 0;)
    {
        if (arr1[j] < arr2[k])
        {
            if ((arr1[j] + 1) == arr2[k])
            {
                if (arr2[k] <= 0)
                    left = 0;
                else
                    right = 0;
            }
            j++;
        }
        else if (arr1[j] > arr2[k])
        {
            if (arr1[j] == (arr2[k] + 1))
            {
                if (arr1[j] >= 0)
                    right = 0;
                else
                    left = 0;
            }
            k++;
        }
        else
        {
            if (arr1[j] < 0)
                left = 0;
            else
                right = 0;
            j++, k++;
        }
    }
    int result = left + right;
    if (result == 4)
        result = 3;
    if (left == 1)
        if (low == 1)
        {
            if (right == 1)
                if (high == 1)
                    if (mid == 1)
                        result = 0;
                    else
                        result = 1;
                else if (mid == 1)
                    result = 1;
                else
                    result = 2;
            else if (right == 2)
            {
                if (mid == 1)
                    result = 1;
                else
                    result = 2;
            }
            else
            {
                if (mid == 1)
                    result = 0;
            }
        }
        else
        {
            if (right == 1)
            {
                if (high == 1)
                    if (mid == 1)
                        result = 1;
            }
            else if (right == 2)
            {
                if (mid == 1)
                    result = 2;
            }
        }
    else if (left == 2)
    {
        if (right == 1)
        {
            if (high == 1)
                if (mid == 1)
                    result = 1;
                else
                    result = 2;
            else if (mid == 1)
                result = 2;
        }
        else if (right == 2)
        {
            if (mid == 1)
                result = 2;
        }
        else
        {
            if (mid == 1)
                result = 1;
        }
    }
    else
    {
        if (mid == 1 && right == 2)
            result = 1;
        else if (mid == 1 && right == 1 && high == 1)
            result = 0;
    }
    cout << result << endl;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
